//获得所有站点信息
function getAllSiteinfo(){
    var tbody=window.document.getElementById("site_data");
    $.ajax({
        url:"../../site/all",
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i)+1;
                    str +="<tr>"+"" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ j +"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].ownId+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].name+"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+convertTypeToString(tdata[i].type)+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" +
                        "<input type='button' value='编辑' id='edit_button'+" + tdata[i].id + " onclick='editButtonClick(" + tdata[i].id + ")'" +"</input>"+
                        "<input type='button' value='删除' id='del_button'+" + tdata[i].id + " onclick='deleteButtonClick( " + tdata[i].id + ")'" +"</input>"+
                        "</td>"+
                        "</tr>";
                    tbody.innerHTML =str;
                }
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function convertTypeToString(type) {
    if (type == 0) {
        return "自有屏";
    }
    if (type == 1) {
        return "非自有屏";
    }
}


function addButtonClick() {
    location.href="./site_add.html";
}

function addSubmitButtonClick() {
    var fdata = $("#site_add_form").serialize();
    $.param(fdata);
    //console.log(fdata);
    $.ajax({
        url:"../../site/new",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./tab.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });
}


//      删除已选商品函数
function deleteButtonClick(id){
    var fdata = $("#sites_form").serialize();
    $.param(fdata);
    //console.log(fdata);
    $.ajax({
        url:"../../site/deletion/" + id,
        type:"GET",
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            //var obj=eval("("+result+")");
            //或者$.parsejson(data);
            if(result.code==0){
                alert(result.message);
                location.href="./tab.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });

}

// 编辑时，获取id号对应的站点信息，并设初值
function setOriginInfo() {
    var url = window.location.href;
    var site_id = getArgsFromHref(url, "id");
    $.ajax({
        url:"../../site/" + site_id,
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                document.getElementById("_id").value=tdata.id;
                document.getElementById("_ownId").value=tdata.ownId;
                document.getElementById("_name").value=tdata.name;
                if (tdata.type == 0) {
                    document.getElementById("_type0").click();
                } else {
                    document.getElementById("_type1").click();
                }
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
    
}

// 点击编辑按钮时，跳转到编辑页面
function editButtonClick(id){
    location.href="./site_edit.html?id=" + id;
}

// 编辑时，点击提交按钮
function editSubmitButtonClick(){
    var fdata = $("#site_edit_form").serialize();
    $.param(fdata);
    //console.log(fdata);
    $.ajax({
        url:"../../site/edit",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./tab.html";
            }else if(result.code==1){
                alert(result.message);
                return;
            }
        }
    });
}

// 获取url中的某个参数
function getArgsFromHref(sHref, sArgName)
{
    var args = sHref.split("?");
    var retval = "";
    if(args[0] == sHref) /*参数为空*/
    {
        return retval; /*无需做任何处理*/
    }
    var str = args[1];
    args = str.split("&");
    for(var i = 0; i < args.length; i ++)
    {
        str = args[i];
        var arg = str.split("=");
        if(arg.length <= 1) continue;
        if(arg[0] == sArgName) retval = arg[1];
    }
    return retval;
}
